/**
 * 
 */
package com.chinasofti.project.web;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.chinasofti.project.common.result.ExecuteResult;
import com.chinasofti.project.dal.model.BzShoppingCart;
import com.chinasofti.project.dal.model.custom.BzProduct;
import com.chinasofti.project.service.product.ShopcartService;
import com.chinasofti.project.web.common.ControllerBase;
import com.chinasofti.project.web.form.ShopcartMessageForm;

/**
 * @author yanghuailong
 * 
 * @Date 2018年9月27日下午3:22:42
 */
@Controller
public class ShopcartController extends ControllerBase {

	@Autowired
	private ShopcartService shopcartService;

	/**
	 * 添加到购物车
	 * 
	 * @param productId
	 * @param session
	 * @return
	 */
	@RequestMapping("addShopcart")
	@ResponseBody
	private ExecuteResult addShopcart(ShopcartMessageForm shopcartMessageForm, HttpSession session) {
		System.out.println(shopcartMessageForm);
		int userId = (int) session.getAttribute("userId");
		BzShoppingCart bzShoppingCart = new BzShoppingCart();
		BeanUtils.copyProperties(shopcartMessageForm, bzShoppingCart);
		bzShoppingCart.setUserId(userId);
		int shoppintCartNum = shopcartService.addShopcart(bzShoppingCart);
		session.setAttribute("shoppintCartNum", shoppintCartNum);
		return ExecuteResult.successResult(shoppintCartNum);
	}

	/**
	 * 进入购物车页面
	 * 
	 * @param session
	 * @param mm
	 * @param shopcartFrom
	 * @return
	 */
	@RequestMapping("toBreezeShopcart")
	private String toBreezeShopcart(HttpSession session, ModelMap mm) {
		// shopcartFrom.setUserId((int)session.getAttribute("userId"));
		int userId = (int) session.getAttribute("userId");
		List<BzProduct> bzProducte = shopcartService.selectByUserId(userId);
		mm.put("bzProducte", bzProducte);
		return "breeze_shopcart";
	}

	/**
	 * 删除购物车数据
	 * 
	 * @param productId
	 * @param session
	 * @param shopcartFrom
	 * @param mm
	 * @return
	 */
	@RequestMapping("deletShopcart")
	@ResponseBody
	private ExecuteResult deletShopcart(BzShoppingCart bzShoppingCart, HttpSession session, ModelMap mm) {
		bzShoppingCart.setUserId((int) session.getAttribute("userId"));
		shopcartService.deletShopcart(bzShoppingCart);
		int shoppintCartNum = (int) session.getAttribute("shoppintCartNum");
		session.setAttribute("shoppingCartNum", shoppintCartNum - 1);
		return ExecuteResult.successResult();
	}

	@RequestMapping("updateShoppcart")
	@ResponseBody
	private ExecuteResult updateShoppcart(BzShoppingCart bzShoppingCart, HttpSession session) {
		System.out.println(bzShoppingCart);
		bzShoppingCart.setUserId((int) session.getAttribute("userId"));
		shopcartService.updateShoppcart(bzShoppingCart);
		return ExecuteResult.successResult();

	}

}
